//
//  CAKeyFrameAnimationViewController.m
//  CoreAnimation
//
//  Created by 张国文 on 16/10/26.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "CAKeyFrameAnimationViewController.h"

@interface CAKeyFrameAnimationViewController ()

@property (nonatomic, strong) UIView *backgroundView;

@property (nonatomic, strong) CALayer *triangle;

@property (nonatomic, strong) UIBezierPath *bezierpath;

@property (nonatomic, strong) CALayer *imageLayer;

@end

@implementation CAKeyFrameAnimationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    // 我们创建一个三角形沿着二次贝塞尔曲线飞行的动画
    
    self.backgroundView = [[UIView alloc] initWithFrame:self.view.frame];
    self.backgroundView.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.5];
    [self.view addSubview:_backgroundView];
    
    self.bezierpath = [[UIBezierPath alloc] init];
    [self.bezierpath moveToPoint:CGPointMake(30, 160)];
    [self.bezierpath addCurveToPoint:CGPointMake(330, 160) controlPoint1:CGPointMake(105, 10) controlPoint2:CGPointMake(255, 310)];
    
    CAShapeLayer *pathLayer = [CAShapeLayer layer];
    pathLayer.strokeColor = [UIColor redColor].CGColor;
    pathLayer.fillColor = [UIColor clearColor].CGColor;
    [pathLayer setLineWidth:2.0f];
    [pathLayer setLineCap:@"kCALineCapRound"];
    pathLayer.path = self.bezierpath.CGPath;
    [self.backgroundView.layer addSublayer:pathLayer];
    
 
    
    self.triangle = [CALayer layer];
    self.triangle.contents = (__bridge id _Nullable)([UIImage imageWithName:@"蒙版2"].CGImage);
    self.triangle.bounds = CGRectMake(0, 0, 30, 30);
    self.triangle.position = CGPointMake(30, 160);
    [self.backgroundView.layer addSublayer:self.triangle];
    
    
    self.imageLayer = [CALayer layer];
    self.imageLayer.bounds = CGRectMake(0, 0, 100, 100);
    self.imageLayer.position = CGPointMake(self.view.center.x, 300);
    self.imageLayer.contents = (__bridge id _Nullable)([UIImage imageWithName:@"snowMan"].CGImage);
    [self.backgroundView.layer addSublayer:_imageLayer];
    
    
    UIButton *startButton = [UIButton buttonWithType:UIButtonTypeCustom];
    startButton.frame = CGRectMake(50, 400, 150, 30);
    [startButton setTitle:@"开始" forState:UIControlStateNormal];
    [startButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [startButton addTarget:self action:@selector(start:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:startButton];

    UIButton *rotateButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rotateButton.frame = CGRectMake(50, 450, 150, 30);
    [rotateButton setTitle:@"旋转" forState:UIControlStateNormal];
    [rotateButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [rotateButton addTarget:self action:@selector(rotate:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:rotateButton];
}

- (void)start:(UIButton *)button
{
    // 关键帧动画
    CAKeyframeAnimation *keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    keyAnimation.duration  = 4.0;
    keyAnimation.path = self.bezierpath.CGPath;
    keyAnimation.rotationMode = kCAAnimationRotateAuto; // 沿着轨迹的切线进行移动，不会减速；kCAAnimationRotateAutoReverse会减速
    keyAnimation.fillMode = kCAFillModeForwards;
    keyAnimation.removedOnCompletion = NO;
    [self.triangle addAnimation:keyAnimation forKey:@"changeAnimation"];
    
}

- (void)rotate:(UIButton *)button
{
    
    /**
     *  用transform.rotation而不是transform做动画的好处如下：
     
     1、我们可以不通过关键帧一步旋转多于180度的动画。
     2、可以用相对值而不是绝对值旋转（设置byValue而不是toValue）。
     3、可以不用创建CATransform3D，而是使用一个简单的数值来指定角度。
     4、不会和transform.position或者transform.scale冲突（同样是使用关键路径来做独立的动画属性）。
     */
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    animation.duration = 2.0;
    animation.byValue = @(M_PI  * 2);
    [self.imageLayer addAnimation:animation forKey:@""];
    
    
}



@end
